local skel = fk.CreateSkill{
  name = "kqf__dihun",
}
Fk:loadTranslationTable{
  ["kqf__dihun"] = "涤魂",
  [":kqf__dihun"] = "当你受到伤害后，若受伤前后，你体力值的排行未变化，你可以弃置两张红色牌，回复1点体力。"..
  "若弃置前后，你手牌数的排行未变化，你再移动场上一张牌。",

  ["#kqf__dihun"] = "涤魂：你可以弃置两张红色牌，回复1点体力",

  ["#kqf__dihun_ask"] = "涤魂：请移动场上一张牌",

  ["$kqf__dihun1"] = "心有灵犀，面如不老之椿。",
  ["$kqf__dihun2"] = "驻颜有术，此间永得芳容。",
}

skel:addEffect(fk.DamageInflicted, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name)
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local hplist = {}
      for _,p in ipairs(room:getAlivePlayers()) do
        table.insertIfNeed(hplist,p.hp)
      end
      table.sort(hplist)
      for e,i in ipairs(hplist) do
        if i == player.hp then
          local n = #hplist + 1 - e
          room:setPlayerMark(player,"kqf__dihun_1",n)
          return
        end
      end
  end,
})

skel:addEffect(fk.Damaged, {
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and #player:getCardIds("he") > 1 then
      local room = player.room
      local hplist = {}
      for _,p in ipairs(room:getAlivePlayers()) do
        table.insertIfNeed(hplist,p.hp)
      end
      table.sort(hplist)
      for e,i in ipairs(hplist) do
        if i == player.hp then
          local n = #hplist + 1 - e
          if player:getMark("kqf__dihun_1") == n then
            return true
          end
        end
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local p,c = room:askToChooseCardsAndPlayers(player,{
      min_card_num = 2,
      max_card_num = 2,
      min_num = 0,
      max_num = 0,
      targets = {},
      skill_name = skel.name,
      prompt = "#kqf__dihun",
      pattern = ".|.|diamond,heart",
    })
    if #c == 2 then
      event:setCostData(self,{cards = c})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cardlist = {}
    local aaa,bbb
    for _,p in ipairs(room:getAlivePlayers()) do
      table.insertIfNeed(cardlist,#p:getCardIds("h"))
    end
    table.sort(cardlist)
    for e,i in ipairs(cardlist) do
      if i == #player:getCardIds("h") then
        aaa = #cardlist + 1 - e
      end
    end
    --
    room:throwCard(event:getCostData(self).cards,skel.name,player)
    room:recover{
      who = player,
      num = 1,
      skillName = skel.name,
    }
    --
    cardlist = {}
    for _,p in ipairs(room:getAlivePlayers()) do
      table.insertIfNeed(cardlist,#p:getCardIds("h"))
    end
    table.sort(cardlist)
    for e,i in ipairs(cardlist) do
      if i == #player:getCardIds("h") then
        bbb = #cardlist + 1 - e
      end
    end
    if aaa ~= bbb then return end
    local ppp = room:askToChooseToMoveCardInBoard(player, {
        prompt = "#kqf__dihun_ask",
        skill_name = skel.name,
        cancelable = false,
    })
    if #ppp == 2 then
      room:askToMoveCardInBoard(player, {
        target_one = ppp[1],
        target_two = ppp[2],
        skill_name = skel.name,
      })
    end
  end
})

return skel